package com.dp.mallchat.common.user.service.adapter;

import com.dp.mallchat.common.user.domain.entity.UserApply;
import com.dp.mallchat.common.user.domain.entity.UserFriend;
import com.dp.mallchat.common.user.domain.user.entity.User;
import com.dp.mallchat.common.user.domain.user.enums.UserApplyEnum;
import com.dp.mallchat.common.user.domain.user.enums.UserApplyReadStatusEnum;
import com.dp.mallchat.common.user.domain.user.enums.UserApprovalStatusEnum;
import com.dp.mallchat.common.user.domain.user.req.FriendApplyReq;
import com.dp.mallchat.common.user.domain.user.resp.FriendUnreadResp;
import com.dp.mallchat.common.user.domain.user.resp.UserFriendResp;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: dupeng
 * @CreateTime: 2024-06-19  19:57
 * @Description: 好用申请适配嘞
 */
public class UserFriendAdapter {

    public static List<UserFriendResp> buildFriendList(List<UserFriend> list, List<User> userList) {
        Map<Long, Integer> map = userList.stream().collect(Collectors.toMap(User::getId, User::getActiveStatus));
        return list.stream().map(item -> {
            UserFriendResp userFriendResp = new UserFriendResp();
            userFriendResp.setUid(item.getFriendUid());
            if (Objects.nonNull(map.get(item.getFriendUid()))) {
                userFriendResp.setActiveStatus(map.get(item.getFriendUid()));
            }
            return userFriendResp;
        }).collect(Collectors.toList());
    }

    public static FriendUnreadResp buildUnreadResp(Integer count) {
        FriendUnreadResp friendUnreadResp = new FriendUnreadResp();
        friendUnreadResp.setUnReadCount(count);
        return friendUnreadResp;
    }

    public static UserApply buildUserApply(Long uid, FriendApplyReq req) {
        UserApply save = new UserApply();
        save.setUid(uid);
        save.setTargetId(req.getTargetUid());
        save.setMsg(req.getMsg());
        save.setType(UserApplyEnum.ADD_FRIEND.getType());
        save.setStatus(UserApprovalStatusEnum.UN_PASS.getStatus());
        save.setReadStatus(UserApplyReadStatusEnum.UN_READ.getType());
        return save;
    }
}
